﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ClickToCall.CustomControls
{
    public partial class ListBox : System.Windows.Forms.ListBox
    {
        public ListBox()
        {
            InitializeComponent();
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Brush highlightedBrush;
            /* 
             * These modification create a bug in the control which causes 
             * the selection area of each index to become offset.  By the 
             * fourth or fifth index this becomes noticeable.  Leaving alone
             * for now since we are only using two indices in this control.
             */
            int ybound = e.Bounds.Y + ((e.Index * 3) == 0 ? 1 : (e.Index * 3));
            if ((e.State & DrawItemState.Selected) > 0)
                highlightedBrush = new System.Drawing.SolidBrush(Color.FromArgb(251, 232, 166));
            else
                highlightedBrush = Brushes.White;

            //resize default bounds
            Rectangle r = new Rectangle(e.Bounds.X, ybound, e.Bounds.Width, e.Bounds.Height + 3);
            e.Graphics.FillRectangle(highlightedBrush, r);
            
            if (e.Index > -1)
                e.Graphics.DrawString(
                    this.Items[e.Index].ToString(),
                    e.Font, 
                    Brushes.Black,
                    e.Bounds.X,
                    ybound);

            base.OnDrawItem(e);
        }
    }
}
